<?php

namespace App\Http\Requests;

use App\Rules\MobileRule;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class AdminPost extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $rules = [];

        if ($this->has('id') || $this->isMethod('put')) { // 账户信息修改
            $this->rulesForUpdate($rules);
        } else if ($this->isMethod('post')) { // 添加账户信息
            $this->rulesForAdd($rules);
        } else if ($this->isMethod('patch')) {
            $this->rulesForPatch($rules);
        }

        return $rules;
    }

    /**
     * 添加用户表单验证规则
     * @param $rules
     */
    private function rulesForAdd (&$rules)
    {
        // 验证账号
        $rules['mobile'] = [
            new MobileRule(),
            Rule::unique('users')->where(function ($query) {
                $query->where('type', 1);
            })
        ];

        // 验证邮箱
        if ($this->has('email')) {
            $rules['email'] = [
                'email',
                Rule::unique('users')->where(function ($query) {
                    $query->where('type', 1);
                })
            ];
        }

        if ($this->has('name')) {
            $rules['name'] = [
                'required',
                'alpha_dash',
                Rule::unique('users')->where(function ($query) {
                    $query->where('type', 1);
                }),
                'min:3',
                'max:16'
            ];
        }

        // 验证真实姓名
        if ($this->has('realname')) {
            $rules['realname'] = [ 'required', 'string', 'min:2', 'max:8' ];
        }

        // 验证昵称
        if ($this->filled('nickname')) {
            $rules['nickname'] = ['required', 'string', 'min:2', 'max:8'];
        }

        if ($this->has('is_effect')) {
            $rules['is_effect'] = 'boolean';
        }

        if ($this->has('sex')) {
            $rules['sex'] = 'required|in:0,1,2';
        }

        if ($this->has('source')) {
            $rules['source'] = 'required:string';
        }

        // 验证用户密码
        $rules['password'] = "required|string|max:18|min:6";
        $rules['re_password'] = 'same:password';
    }

    private function rulesForUpdate (&$rules)
    {
        // 验证手机
        if ($this->has('mobile')) {
            $rules['mobile'] = [
                new MobileRule(),
                Rule::unique('users')->where(function ($query) {
                    $query->where('type', 1);
                })->ignore($this->id)
            ];
        }

        // 验证邮箱账号
        if ($this->has('email')) {
            $rules['email'] = [
                'email',
                Rule::unique('users')->where(function ($query) {
                    $query->where('type', 1);
                })->ignore($this->id)
            ];
        }

        // 验证真实姓名
        if ($this->filled('realname')) {
            $rules['realname'] = [ 'required', 'string', 'min:2', 'max:8' ];
        }

        // 验证昵称
        if ($this->filled('nickname')) {
            $rules['nickname'] = [ 'required', 'string', 'min:2', 'max:8' ];
        }

        // 验证账户是否有效
        if ($this->has('is_effect')) {
            $rules['is_effect'] = 'boolean';
        }

        // 验证性别
        if ($this->has('sex')) {
            $rules['sex'] = 'required|in:0,1,2';
        }

        // 验证用户密码
        if ($this->filled('password')) {
            $rules['password'] = "required|string|max:18|min:6";
            $rules['re_password'] = 'same:password';
        }
    }

    private function rulesForPatch (&$rules)
    {
        $user_id = $this->id;

        // 验证手机
        if ($this->has('mobile')) {
            $rules['mobile'] = [
                new MobileRule(),
                Rule::unique('users')->where(function ($query) {
                    $query->where('type', 1);
                })->ignore($user_id)
            ];
        }

        // 验证邮箱账号
        if ($this->has('email')) {
            $rules['email'] = [
                'email',
                Rule::unique('users')->where(function ($query) {
                    $query->where('type', 1);
                })->ignore($user_id)
            ];
        }

        // 验证真实姓名
        if ($this->has('realname')) {
            $rules['realname'] = [ 'required', 'string', 'min:2', 'max:8' ];
        }

        // 验证昵称
        if ($this->filled('nickname')) {
            $rules['nickname'] = [ 'required', 'string', 'min:2', 'max:8' ];
        }

        // 验证账户是否有效
        if ($this->has('is_effect')) {
            $rules['is_effect'] = 'boolean';
        }

        // 验证性别
        if ($this->has('sex')) {
            $rules['sex'] = 'required|in:0,1,2';
        }

        // 验证用户密码
        if ($this->filled('password')) {
            $rules['password'] = "required|string|max:18|min:6";
            $rules['re_password'] = 'same:password';
        }
    }

    public function attributes()
    {
        return [
            'name' => '用户名',
            'nickname' => '昵称'
        ];
    }
}
